home *** CD-ROM | disk | FTP | other *** search
- Path: nyssa.swt.edu!LN16674
- From: ln16674@nyssa.swt.edu (Leland Newsom)
- Newsgroups: comp.lang.c
- Subject: Re: reversing a string
- Date: 10 Apr 1996 01:07:52 GMT
- Organization: Southwest Texas State University
- Message-ID: <4kf1l8$b18@central.server.swt.edu>
- References: <4k6cjl$j8f@central.server.swt.edu> <4kb1s7$6eu@ibm32.perftech.com> <829058514snz@genesis.demon.co.uk>,<1996Apr9.144011.117444@kuhub.cc.ukans.edu>
- Reply-To: ln16674@nyssa.swt.edu
- NNTP-Posting-Host: nyssa.swt.edu
-
- In article <1996Apr9.144011.117444@kuhub.cc.ukans.edu>, anh@kuhub.cc.ukans.edu (TRAN PHAN ANH) writes:
- >Something like this? Or is the s2 argument considered an extra variable.
- >
- >Anh
-
- Yes, s2 would be considered an extra variable.
- Leland
-
-
- >
- >
- >#include <stdio.h>
- >#include <string.h>
- >
- >void reverse(char *s1, char *s2)
- >{
- > if(s1>=s2)
- > return;
- >
- > *s1^=*s2;
- > *s2^=*s1;
- > *s1^=*s2;
- >
- > reverse(s1+1,s2-1);
- >}
- >
- >
- >int main(int argc, char *argv[])
- >{
- > printf("Arg: %s\n",argv[1]);
- >
- > reverse(argv[1],&argv[1][strlen(argv[1])-1]);
- >
- > printf("Result: %s\n",argv[1]);
- >}
- >
- >
- >
- >
- >
- >In article <829058514snz@genesis.demon.co.uk>, Lawrence Kirby <fred@genesis.demon.co.uk> writes:
- >> In article <4kb1s7$6eu@ibm32.perftech.com>
- >> murf@perftech.com "John Murphy" writes:
- >>
- >>>In article <4k6cjl$j8f@central.server.swt.edu>, ln16674@nyssa.swt.edu
- >>>says...
- >>>>
- >>>>I have a challenge from a friend of mine. He wanted me to reverse a string
- >>>>with recursion without using any additional variables or loops. I got mine
- >>>>to work by using exclusive or, but I needed an additional variable. Can
- >>>>someone help with this problem without using the additional variable?
- >>>>
- >>>>Thanks
- >>>You can swap two variables, x and y, with the following series of exclusive
- >>>or's:
- >>> x ^= y;
- >>> y ^= x;
- >>> x ^= y;
- >>
- >> From what he wrote I believe Leland was already doing that. The problem is
- >> to write the entire function without using an additional variable. It
- >> can be done! :-)
- >>
- >> --
- >> -----------------------------------------
- >> Lawrence Kirby | fred@genesis.demon.co.uk
- >> Wilts, England | 70734.126@compuserve.com
- >> -----------------------------------------
-